jQuery(document).ready(function ($) {

	$.facebox.settings.closeImage = wvSettings.wvurl + '/js/closelabel.png';
	$.facebox.settings.loadingImage = wvSettings.wvurl + '/js/loading.gif';

	// Facebox hook, make the audio work
	$(document).bind('reveal.facebox', function() {
		var audioPlaceholder = $("#wv_lightbox .audioplayer_container span");
		audioPlaceholder.attr('id', 'facebox_' + audioPlaceholder.attr('id'));
	})

    //Replace vimeo with img
    var re = /vimeo.com\/(video\/)?(\d+)\/?/i;
    if (re.test($('.vvqvimeo iframe').attr('src'))) {
        var url = "http://vimeo.com/api/v2/video/" + RegExp.$2 + ".json?callback=showThumb";
        $('.vvqvimeo').before('<script type="text/javascript" src="' + url + '"></script>');
    }

    //Open facebox on video
    $('.wv-video-widget a, .vvqbox img, .wv-video-comment').click(function () {
        var post_id = $('.wv-video-widget a').attr('id').split('-')[1];
        $.facebox(function () {
            $.get(wvSettings.ajaxurl, {action: 'wv_widget', post_id: post_id, nonce: wvSettings.nonce}, function (data) {
                $.facebox(data);
            })
        })
        return false;
    })

    //Open facebox on audio
    $('#wv-votd-musicwidget img, .wv-song-widget a, .wv-song-comment').click(function () {
        var post_id = $('.wv-song-widget a').attr('id').split('-')[1];
        $.facebox(function () {
            $.get(wvSettings.ajaxurl, {action: 'wv_widget', post_id: post_id, nonce: wvSettings.nonce}, function (data) {
                $.facebox(data);
                var length = $('.wv-entry-content img').attr('width');
                var $player = $('#wv_lightbox .audioplayer_container object');
                if (length) $player.attr('width', length + 'px');
                setTimeout(function () {
                    AudioPlayer.open($player.attr('id'), 0);
                }, 1000);
            })
        })
        return false;
    })


    //Resize img
//    $('.vvqbox.vvqyoutube:has(img)').css({width: wvSettings.video_width, height: wvSettings.video_height});
    $('.vvqbox, .vvqbox img, #wv-votd-musicwidget img').css({width: '100%', height: 'auto'});

    //Ajax comment
    $result = $('<div></div>').appendTo('#commentform');
    $('#commentform').live('submit', function (e) {
        $(this).ajaxSubmit({
            beforeSubmit: function (formData, jqForm, options) {
                $('#submit').css({float: 'left'});
                $('#wv_ajax_result').appendTo('.form-submit').css({color: 'blue'}).text('Submiting...');
            },
            success: function (responseText, statusText, xhr, $form) {
                $result.css({color: 'blue' }).text('Comment successful.');
                $('#wv_ajax_result').css({color: 'blue' }).text('Comment successful.');
            },
            error: function (xhr) {
                $result.css({color: 'red' }).text('Comment error.');
                $('#wv_ajax_result').css({color: 'red' }).text('Comment failed.');
            }
        })
        return false;
    })
})

function showThumb(data) {
    jQuery('.wv-widget-body .vvqbox.vvqvimeo').empty();
    jQuery('<img title="vimeo"/>')
            .attr('src', data[0].thumbnail_medium)
            .css({width: '100%', height: 'auto', cursor: 'pointer'})
            .appendTo('.wv-widget-body .vvqbox.vvqvimeo')
            .click(function () {
                var post_id = jQuery('.wv-video-widget a').attr('id').split('-')[1];
                jQuery.facebox(function () {
                    jQuery.get(wvSettings.ajaxurl, {action: 'wv_widget', post_id: post_id, nonce: wvSettings.nonce}, function (data) {
                        jQuery.facebox(data);
                    })
                })
            })
}